<?php

class CateController extends Controller
{
	/**
	 * @var string the default layout for the views. Defaults to '//layouts/column2', meaning
	 * using two-column layout. See 'protected/views/layouts/column2.php'.
	 */
	public $layout='//layouts/column2';

	/**
	 * @return array action filters
	 */
	public function filters()
	{
		return array(
			'accessControl', // perform access control for CRUD operations
		);
	}

	/**
	 * Specifies the access control rules.
	 * This method is used by the 'accessControl' filter.
	 * @return array access control rules
	 */
	public function accessRules()
	{
		return array(
			array('allow',  // allow all users to perform 'index' and 'view' actions
				'actions'=>array('index','view','list'),
				'users'=>array('*'),
			),
			array('allow', // allow authenticated user to perform 'create' and 'update' actions
				'actions'=>array('create','update'),
				'users'=>array('@'),
			),
			array('allow', // allow admin user to perform 'admin' and 'delete' actions
				'actions'=>array('admin','delete'),
				'users'=>array('admin'),
			),
			array('deny',  // deny all users
				'users'=>array('*'),
			),
		);
	}

	/**
	 * Displays a particular model.
	 * @param integer $id the ID of the model to be displayed
	 */
	public function actionView($id)
	{
		$this->render('view',array(
			'model'=>$this->loadModel($id),
		));
	}

    public function actionList()
    {
        $cid=Yii::app()->request->getParam('cid', 0);              //子类
        $pid=Yii::app()->request->getParam('pid', 0);              //父类

        $type=Yii::app()->request->getParam('type', 1);            //父类

        if($pid==0){
            throw new CHttpException(404,'找不到相应文章分类');
        }

        if($cid==0 && $pid>0){
            //找到默认的子分类
            $model=Cate::model()->with('parent')->find('`t`.`pid`=:pid', array(':pid'=>$pid));
            if(isset($model->cid) && $model->cid>0){
               $cid=$model->cid;
            }
        }
        if($cid==0){
            throw new CHttpException(404,'文章列表不存在');
        }

        $this->render('list',array(
            'pid'=>$pid,
            'cid'=>$cid,
            'current'=>Cate::model()->with('parent')->findByPk($cid),
            'children'=>Cate::model()->with('children')->findAll('`t`.`pid`=:pid', array(':pid'=>$pid)),
            'type'=>$type
        ));
    }

	/**
	 * Creates a new model.
	 * If creation is successful, the browser will be redirected to the 'view' page.
	 */
	public function actionCreate()
	{
		$model=new Cate;

		// Uncomment the following line if AJAX validation is needed
		// $this->performAjaxValidation($model);

		if(isset($_POST['Cate']))
		{
			$model->attributes=$_POST['Cate'];
			if($model->save())
				$this->redirect(array('user/article','cid'=>$model->pid));
		}

		$this->render('create',array(
			'model'=>$model,
		));
	}

	/**
	 * Updates a particular model.
	 * If update is successful, the browser will be redirected to the 'view' page.
	 * @param integer $id the ID of the model to be updated
	 */
	public function actionUpdate($id)
	{
		$model=$this->loadModel($id);

		// Uncomment the following line if AJAX validation is needed
		// $this->performAjaxValidation($model);

		if(isset($_POST['Cate']))
		{
			$model->attributes=$_POST['Cate'];
			if($model->save())
				$this->redirect(array('view','id'=>$model->cid));
		}

		$this->render('update',array(
			'model'=>$model,
		));
	}

	/**
	 * Deletes a particular model.
	 * If deletion is successful, the browser will be redirected to the 'admin' page.
	 * @param integer $id the ID of the model to be deleted
	 */
	public function actionDelete($id)
	{
		if(!Yii::app()->request->isPostRequest)
		{
			// we only allow deletion via POST request
            $cid = $this->loadModel($id)->cid;
            if(Article::model()->find('cid=:cid',array(':cid'=>$cid))){
                throw new CHttpException(400,'分类列表不为空,不能直接删除');
            }
			$this->loadModel($id)->delete();

			// if AJAX request (triggered by deletion via admin grid view), we should not redirect the browser
			if(!isset($_GET['ajax']))
				$this->redirect(isset($_GET['returnUrl']) ? CHtml::decode($_GET['returnUrl']) : array('admin'));
		}
		else
			throw new CHttpException(400,'Invalid request. Please do not repeat this request again.');
	}

	/**
	 * Lists all models.
	 */
	public function actionIndex()
	{
		$dataProvider=new CActiveDataProvider('Cate');
		$this->render('index',array(
			'dataProvider'=>$dataProvider,
		));
	}

	/**
	 * Manages all models.
	 */
	public function actionAdmin()
	{
		$model=new Cate('search');
		$model->unsetAttributes();  // clear any default values
		if(isset($_GET['Cate']))
			$model->attributes=$_GET['Cate'];

		$this->render('admin',array(
			'model'=>$model,
		));
	}

	/**
	 * Returns the data model based on the primary key given in the GET variable.
	 * If the data model is not found, an HTTP exception will be raised.
	 * @param integer the ID of the model to be loaded
	 */
	public function loadModel($id)
	{
		$model=Cate::model()->findByPk($id);
		if($model===null)
			throw new CHttpException(404,'The requested page does not exist.');
		return $model;
	}

	/**
	 * Performs the AJAX validation.
	 * @param CModel the model to be validated
	 */
	protected function performAjaxValidation($model)
	{
		if(isset($_POST['ajax']) && $_POST['ajax']==='cate-form')
		{
			echo CActiveForm::validate($model);
			Yii::app()->end();
		}
	}
}
